﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Grishko.UI;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Infrastructure;

/// <summary>
/// Комбинация - панель фильтра и редактор. Особенность - редактору 
/// подсовавается копия текущего фильтра панели . Наружу контрол представлен как редактор фильтра. 
/// </summary>

public partial class Ctl_ctlDefaultFilterPanel : System.Web.UI.UserControl,IFilterEditor 
{
    private const string CloneFilterKey = "CloneFilterKey";

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);        
        IitControls();
        if (!IsPostBack)
            CacheTools.ResetSessionCache(CloneFilterKey); 
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            
            DataBind();
            ImChange();
         
        }       

    }

    public  FilterPanel _FilterPanel
    {
        get
        {
            return FilterPanel1;
        }
    }


    protected virtual IFilterPanel FilterPanel
    {
        get { return FilterPanel1; }
    }

    protected virtual IFilterEditor FilterEditor
    {
        get { return this; }
    }

    Filter _FilterClone = null; 
    protected Filter GetCloneFilterDataSource()
    {
        if (null != _FilterClone)
                return _FilterClone;

        if (CacheTools.IsSessionCacheObject(CloneFilterKey))        
            _FilterClone = (Filter)CacheTools.GetSessionCacheObect(CloneFilterKey);        
        else
        {
            Filter BaseFilter = FilterPanel.DataSource();
            if (null == BaseFilter)
                return null;

            _FilterClone = (Filter)BaseFilter.Clone();
            CacheTools.SetSessionCacheObject (CloneFilterKey,_FilterClone);        
        }
        
        return _FilterClone;
    }

    public bool ShowEditor
    {
        get
        {

            object text = ViewState["ShowEditor"];
            if (null == text)
                return false;
            else
                return Convert.ToBoolean(text);
        }
        set
        {            
            ViewState["ShowEditor"] = value;
        }
    }
   
    private void IitControls()
    {
        FilterEditor.DataSource = GetCloneFilterDataSource;
        ctlFilterEditor1.DataSource = GetCloneFilterDataSource;        
   
    }
    
   
    protected void btnShowFilter_Click(object sender, ImageClickEventArgs e)
    {
        ShowEditor = (!ShowEditor);
        DataBind();
        updFilter.Update();
    }

    
    
       
    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        TextBox_PostBack1_KeyDownEnter(null, null);
    }
 
    protected void TextBox_PostBack1_KeyDownEnter(object sender, EventArgs e)
    {
        DataSource().Where.Lexeme = txtLexeme.Text ;        
        
        if (null != ImChange)
                ImChange();
    
    }



    #region Члены IFilterEditor

    public Func<Filter> DataSource
    {
        get;set;        
    }

    public Func<bool> ImChange
    {
        get { return ctlFilterEditor1.ImChange; }
        set { ctlFilterEditor1.ImChange = value; }
    }    

    #endregion
}
